﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Text;

namespace PTA.Web
{
    public class PtaHandler : IHttpHandler, IRequiresSessionState
    {
        private bool isReusable = false;
        protected bool RequestReusable
        {
            get
            {
                return isReusable;
            }
            set
            {
                isReusable = value;
            }
        }
        public bool IsReusable
        {
            get { return isReusable; }
        }

        public void ProcessRequest(HttpContext context)
        {
            #region 处理异步请求
            if (CheckAjaxRequest(context))
            {
                ProcessAjaxRequest(context);
                return;
            }
            #endregion

            #region 处理资源文件请求
            if (CheckAjaxRequest(context))
            {
                ProcessResRequest(context);
                return;
            }
            #endregion

            #region 处理下载文件请求
            if (CheckFileRequest(context))
            {
                ProcessFileRequest(context);
                return;
            }
            #endregion

            #region 其它类型请求
            ProcessCustomRequst(context);
            #endregion
        }

        #region 异步请求
        protected virtual bool CheckAjaxRequest(HttpContext context)
        {
            return (context.Request.Headers["X-Requested-With"] != null);
        }
        protected virtual void ProcessAjaxRequest(HttpContext context)
        {
            AjaxUtility.ProcessRequest(context);
        }
        #endregion

        #region 资源文件请求
        protected virtual bool CheckResRequest(HttpContext context)
        {
            return false;
        }
        protected virtual void ProcessResRequest(HttpContext context)
        {
        }
        #endregion

        #region 下载文件请求
        protected virtual bool CheckFileRequest(HttpContext context)
        {
            return false;
        }
        protected virtual void ProcessFileRequest(HttpContext context)
        {
        }
        #endregion

        #region 其它请求
        public void ProcessCustomRequst(HttpContext context)
        {
        }
        #endregion
    }
}
